const app = getApp()
import BaseModel from '../../utils/common.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",//活动id
    page:1,
    size:6,
    comment_nav:0,//评价切换
    getActivityMes:[], //基本数据
    getActivityTicket:[], //票种
    getActivityTntroductionData:[], //活动简介
    commentBaseMes:"",//评价基本信息
    activityCommentData:[], //评价列表
    formId:"",//表单id
    phone:"", //活动手机号
    ticketId:"",//票种id
    orderid:"",//订单id
    navDataId:1,//导航，1是简介，2是评论
    is_clickTab:0,//是否上拉。0是否，1是上拉
    isOverdue:"",//验证活动是否过期  1:过期 2是未过期
    platform:"", //机型
    footer_more:"",
    isshow:false, //弹窗是否显示
    template: {}, //绘制模板
    posterData:"", //海报数据
    empty_title:"",
    empty_img:"",
    empty_again:"",//重试
    articleContents:"", //文章详情
    register_img:"",  //签到二维码
    companyRole:"",//公司角色
    navType:"",//导航，1全国 2分社
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.navType)
    this.setData({
      navType:options.navType,
      companyRole:app.globalData.companyRole,
      id:options.id,
      platform:wx.getStorageSync('platform') //判断机型
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getActivityMesData(); //获取基本数据
    this.getActivityTntroductionData(); //简介数据
    this.getCourseBaseMesData(); //评价基本信息
    this.getRegisterMesData();//获取签到二维码
    if(this.data.navDataId==2){
      this.getActivityCommentData();
    }
  },
  /*点击切换一级tab*/
  nav_tabSwitch:function(e){
    this.setData({
      navDataId:e.currentTarget.dataset.id,
      page:1,
      is_clickTab:0
    })
    if(this.data.navDataId==1){
      this.getActivityTntroductionData();
    }else if(this.data.navDataId==2){
      this.getActivityCommentData();
    }
  },
  /*评价tab切换*/
  comment_nav:function(e){
    this.setData({
      comment_nav:e.currentTarget.dataset.id,
      page:1,
      is_clickTab:0,
    })
    this.getActivityCommentData();
  },
  /*点击隐藏海报*/
  share_posterHide:function(){
    this.setData({
      isshow:false,
    })
  },
  /*获取评价信息*/
  getCourseBaseMesModel:new BaseModel({
    url:"employees/commentMark",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getCourseBaseMesData(){
    this.getCourseBaseMesModel
        .fetch({
          activity_id:this.data.id,
        })
        .then(res => {
          //成功
          this.setData({
            commentBaseMes:res.data,
          })
        })
        .catch(res => {
          //失败
        })
  },
  /*获取简介数据*/
  getActivityTntroductionModel:new BaseModel({
    url:"employees/introductionActivity",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getActivityTntroductionData(){
    this.getActivityTntroductionModel
        .fetch({
          id:this.data.id
        })
        .then(res => {
          //成功
          this.setData({
            getActivityTntroductionData:res.data,
          })
        })
        .catch(res => {
          //失败
        })
  },
  /*获取评价*/
  getActivityCommentModel:new BaseModel({
    url:"employees/getCommentList",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getActivityCommentData(){
    this.getActivityCommentModel
        .fetch({
          activity_id:this.data.id,
          type:this.data.comment_nav,
          "page": this.data.page,
          "size": this.data.size
        })
        .then(res => {
          //成功
          if(this.data.is_clickTab==0){
            this.setData({
              activityCommentData:res.data,
              empty_title:"暂无数据",
              empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon_new3.0/empty_mes.png",
              empty_again:"",//重试
            })
          }else{
            if(res.data==""||res.data==null){
              this.setData({
                footer_more:"没有更多数据了"
              })
            }else{
              var activityCommentData_list = this.data.activityCommentData;
              for(var i=0;i<res.data.length;i++){
                activityCommentData_list.push(res.data[i])
              }
              this.setData({
                activityCommentData: activityCommentData_list
              })
            }
          }
        })
        .catch(res => {
          this.setData({
            empty_title:"请求失败",
            empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/empty_data.png",
            empty_again:"重试",//重试
          })
        })
  },
  /*获取海报数据*/
  getPosterModel:new BaseModel({
    url:"employees/sharePoster",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getPosterData(){
    this.getPosterModel
        .fetch({
          activityId:this.data.id
        })
        .then(res => {
          //成功
          //console.log(res)
          this.setData({
            posterData:res.data,
          })
          if(this.data.posterData==""){
            wx.showToast({
              title: '海报生成中，约30秒',
              icon: 'none',
              duration: 2000
            })
          }else{
            wx.showLoading({
              title: '海报生成中...',
            });
            this.setData({
              isshow:true,
              template:this.palette()
            })
          }
        })
        .catch(res => {
          //失败
        })
  },
  /*根据id获取基本数据*/
  getActivityMesModel:new BaseModel({
    url:"employees/detailActivity",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getActivityMesData(){
    this.getActivityMesModel
        .fetch({
          id:this.data.id
        })
        .then(res => {
          //成功
          this.setData({
            getActivityMes:res.data,
            phone:res.data.activity_phone,
            isOverdue:res.data.expire,
            articleContents:res.data.detail.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block" ')
                .replace(/<section/g, '<div')
                .replace(/\/section>/g, '\div>')
          })
          /* var contentData = res.data.detail;
           WxParse.wxParse('contentData', 'html', contentData, this,5);*/
        })
        .catch(res => {
          //失败
        })
  },
  /*根据id获取基本数据*/
  getRegisterMesModel:new BaseModel({
    url:"employees/signCode",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getRegisterMesData(){
    this.getRegisterMesModel
        .fetch({
          activity_id:this.data.id
        })
        .then(res => {
          //成功
          this.setData({
            register_img:res.data
          })
        })
        .catch(res => {
          //失败
        })
  },
  /*问答*/
  answers:function(){
    wx.navigateTo({
      url: './activity_questions?id='+this.data.id
    })
  },
  /*预览大图*/
  lookBigImg:function(e){
    wx.previewImage({
      current: e.currentTarget.dataset.img, // 当前显示图片的http链接
      urls: this.data.getActivityTntroductionData.Activity_img.content_img // 需要预览的图片http链接列表
    })
  },
  /*签到二维码 预览图*/
  shareRegister:function(e){
    wx.previewImage({
      current: e.currentTarget.dataset.img, // 当前显示图片的http链接
      urls: [e.currentTarget.dataset.img] // 需要预览的图片http链接列表
    })
  },
  /*分享海报*/
  sharePoster:function(){
    this.getPosterData();//海报数据
  },
  onImgOK(e) {
    wx.hideLoading();
    this.imagePath = e.detail.path;
  },
  /*长按保存图片*/
  longTap: function(e){
    var that =this;
    wx.showModal({
      title: '提示',
      content: '保存分享',
      success(res) {
        if (res.confirm) {
          wx.saveImageToPhotosAlbum({
            filePath: that.imagePath,
            success(res) {
              wx.showToast({
                title: '保存成功',
                icon: 'success',
                duration: 2000
              })
            },
            fail(res) {
              wx.showToast({
                title: '保存失败，请重试',
                icon: 'none',
                duration: 2000
              })
            },
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /*保存图片*/
  saveImage() {
    wx.saveImageToPhotosAlbum({
      filePath: this.imagePath,
      success(res) {
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000
        })
      },
      fail(res) {
        wx.showToast({
          title: '保存失败，请重试',
          icon: 'none',
          duration: 2000
        })
      },
    });
  },
  /*禁止页面滚动*/
  preventTouchMove:function(e) {

  },
  palette() {
    return ({
      width: '630rpx',
      height: '1020rpx',
      background: '/images/share_posterBg.jpg',
      views: [
        {
          type:"image",  //头像
          url: this.data.posterData.staff_head,
          css:[{
            top:"120rpx",
            left:"262rpx",
            width:"110rpx",
            height:"110rpx",
            borderRadius:"66rpx",
            borderWidth:0
          }]
        },
        {
          type:"text",
          text:this.data.posterData.staff_name,
          css:[{
            top:"260rpx",
            left:"190rpx",
            fontSize:"28rpx",
            fontWeight: 'bold',
            color:"#666"
          }]
        },
        {
          type:"text",
          text:"邀请您一起学习",
          css:[{
            top:"260rpx",
            left:"290rpx",
            fontSize:"26rpx",
            color:"#666"
          }]
        },
        {
          type:"text",
          text:this.data.posterData.activity_title,
          css:[{
            top:"320rpx",
            align: 'center',
            width: '400rpx',
            left: '310rpx',
            maxLines: 2,
            fontSize:"32rpx",
            lineHeight:"46rpx",
            fontWeight: 'bold'
          }]
        },
        {
          type:"image",  //活动图
          url: this.data.posterData.activity_img,
          css:[{
            top:"430rpx",
            left:"120rpx",
            width:"390rpx",
            height:"190rpx",
            borderRadius:"10rpx",
            borderWidth:0
          }]
        },
        {
          type:"image",  //二维码图片
          url: this.data.posterData.code,
          css:[{
            bottom:"150rpx",
            left:"250rpx",
            width:"160rpx",
            height:"160rpx",
          }]
        },
        {
          type:"text",
          text: "扫码查看此课程",
          css:[{
            align: 'center',
            bottom:"110rpx",
            left:"330rpx",
            fontSize:"20rpx",
            color:"#666"
          }]
        }
      ],
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  //重试
  again_request:function(){
    this.setData({
      is_clickTab:0,
      page:1
    })
    if(this.data.navDataId==2){
      this.getActivityCommentData();
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      is_clickTab:0,
      page:1
    })
    this.getActivityMesData(); //获取基本数据
    this.getActivityTntroductionData(); //简介数据
    this.getCourseBaseMesData(); //评价基本信息
    this.getRegisterMesData();//获取签到二维码
    if(this.data.navDataId==2){
      this.getActivityCommentData();
    }
    wx.stopPullDownRefresh(); //停止下拉
  },
  /**
   * 页面相关事件处理函数--监听用户上啦动作
   */
  onReachBottom: function () {
    var that = this;
    that.data.page+=1;
    this.setData({
      is_clickTab:1  //上拉加载标识
    })
    if(this.data.navDataId==2){
      this.getActivityCommentData();
    }
  },


})